package com.smart.service;


import com.alibaba.fastjson2.JSONObject;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.file.UploadBody;
import com.smart.common.entities.file.UploadResult;
import com.smart.common.exception.SmartException;
import com.smart.manager.system.repository.ConfigRepository;
import com.smart.service.oss.AliYunFileHandler;
import com.smart.service.oss.MinioFileHandler;
import com.smart.service.oss.QiNiuYunFileHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传服务
 *
 * @author Administrator
 * @date 2022/09/09
 */
@Service
@RequiredArgsConstructor
public class FileService {


    private final ConfigRepository configRepository;
    private final MinioFileHandler minioFileHandler;

    private final QiNiuYunFileHandler qiNiuYunFileHandler;

    private final AliYunFileHandler aliYunFileHandler;


    /**
     * 获取当前系统默认的存储方案配置
     */
    private String storageSchemeCode() {
        JSONObject jsonObject = configRepository.readCache(SmartConstant.APPLY_OSS_CFG);
        return jsonObject.getString("schemeCode").trim();
    }


    /**
     * 上传文件
     *
     * @param file      文件
     * @param topFolder 顶级文件夹
     * @return {@code UploadResult}
     */
    public UploadResult uploadFile(MultipartFile file, String topFolder, String fileNamePrefix) {

        switch (storageSchemeCode()) {
            case SmartConstant.QINIUYUN_OSS_CFG:
                return qiNiuYunFileHandler.uploadFile(file, topFolder, fileNamePrefix);
            case SmartConstant.MINIO_OSS_CFG:
                return minioFileHandler.uploadFile(file, topFolder, fileNamePrefix);

            case SmartConstant.ALIYUN_OSS_CFG:
                return aliYunFileHandler.uploadFile(file, topFolder, fileNamePrefix);

            default:
                throw new SmartException("上传配置异常:未知的上传策略代码");


        }


    }


    /**
     * 上传图片
     *
     * @param file      文件
     * @param topFolder 顶级文件夹
     * @return {@code UploadResult}
     */
    public UploadResult uploadImg(MultipartFile file, String topFolder, String fileNamePrefix) {

        switch (storageSchemeCode()) {
            case SmartConstant.QINIUYUN_OSS_CFG:
                return qiNiuYunFileHandler.uploadImg(file, topFolder, fileNamePrefix);
            case SmartConstant.MINIO_OSS_CFG:
                return minioFileHandler.uploadImg(file, topFolder, fileNamePrefix);

            case SmartConstant.ALIYUN_OSS_CFG:
                return aliYunFileHandler.uploadImg(file, topFolder, fileNamePrefix);
            default:
                throw new SmartException("上传配置异常:未知的上传策略代码");
        }

    }


    /**
     * Base64上传文件
     *
     * @param dto       dto
     * @param topFolder 顶级文件夹
     * @return {@code UploadResult}
     */
    public UploadResult uploadFile(UploadBody.Base64 dto, String topFolder, String fileNamePrefix,Boolean isImg) {


        switch (storageSchemeCode()) {
            case SmartConstant.QINIUYUN_OSS_CFG:
                return qiNiuYunFileHandler.uploadFile(dto, topFolder, fileNamePrefix);
            case SmartConstant.MINIO_OSS_CFG:
                return minioFileHandler.uploadFile(dto, topFolder, fileNamePrefix);
            case SmartConstant.ALIYUN_OSS_CFG:
                return aliYunFileHandler.uploadFile(dto, topFolder, fileNamePrefix,isImg);
            default:
                throw new SmartException("上传配置异常:未知的上传策略代码");


        }

    }


    /**
     * 文件字节模式上传
     *
     * @param dto       dto
     * @param topFolder 顶级文件夹
     * @return {@code UploadResult}
     */
    public UploadResult uploadFileByByte(UploadBody.ByteFile dto, String topFolder, String fileNamePrefix) {

        switch (storageSchemeCode()) {

            case SmartConstant.QINIUYUN_OSS_CFG:
                return qiNiuYunFileHandler.uploadFile(dto, topFolder, fileNamePrefix);
            case SmartConstant.MINIO_OSS_CFG:
                return minioFileHandler.uploadFile(dto, topFolder, fileNamePrefix);
            case SmartConstant.ALIYUN_OSS_CFG:
                return aliYunFileHandler.uploadFile(dto, topFolder, fileNamePrefix);
            default:
                throw new SmartException("上传配置异常:未知的上传策略代码");


        }


    }



}
